package com.coolgor.axnut.service.airuc.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.axnut.dao.airuc.HeadlineDao;
import com.coolgor.axnut.dto.airuc.HeadlineExecution;
import com.coolgor.axnut.entity.airuc.Headline;
import com.coolgor.axnut.enums.airuc.HeadlineStateEnum;
import com.coolgor.axnut.exceptions.airuc.HeadlineOperationException;
import com.coolgor.axnut.service.airuc.HeadlineService;

@Service
public class HeadlineServiceImpl implements HeadlineService {
	private static Logger log = LoggerFactory.getLogger(HeadlineServiceImpl.class);
	
	@Autowired
	private HeadlineDao headlineDao;
	
	
	@Override
	public Headline getHeadline(Long headlineID) {
		return headlineDao.queryHeadlineById(headlineID);
	}

	@Override
	public HeadlineExecution getHeadlineList(Headline headlineCondition) {
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<Headline> headlineList = headlineDao.queryHeadlineList(headlineCondition);
		//依据相同的查询条件，返回查询结果总数
		int count = headlineDao.queryHeadlineCount(headlineCondition);
		HeadlineExecution result = new HeadlineExecution();
		if (headlineList != null) {
			result.setState(HeadlineStateEnum.SUCCESS.getState());
			result.setStateInfo(HeadlineStateEnum.SUCCESS.getStateInfo());
			result.setHeadlineList(headlineList);
			result.setCount(count);
		} else {
			result.setState(HeadlineStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public HeadlineExecution addHeadline(Headline headline) {
		// 空值判断
		if (headline == null) {
			return new HeadlineExecution(HeadlineStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 赋初始值
			headline.setCreateTime(new Date());
			headline.setLastEditTime(new Date());
			// 添加
			effectedNum = headlineDao.insertHeadline(headline);
			if (effectedNum <= 0) {
				throw new HeadlineOperationException("学院头条信息创建失败");
			} 
		} catch (Exception e) {
			throw new HeadlineOperationException("add Headline error:" + e.getMessage());
		}
		return new HeadlineExecution(HeadlineStateEnum.SUCCESS, headline);
	}

	@Override
	@Transactional
	public HeadlineExecution modifyHeadline(Headline headline) {
		// 空值判断
		if (headline == null) {
			return new HeadlineExecution(HeadlineStateEnum.NULL);
		}
		int effectedNum;
		// 即将更新信息
		try {
			// 赋初始值
			headline.setLastEditTime(new Date());
			// 更新操作
			effectedNum = headlineDao.updateHeadline(headline);
			if (effectedNum <= 0) {
				throw new HeadlineOperationException("学院头条信息更新失败");
			} 
		} catch (Exception e) {
			throw new HeadlineOperationException("Modify Headline Information error:" + e.getMessage());
		}
		return new HeadlineExecution(HeadlineStateEnum.SUCCESS, headline);
	}

	@Override
	@Transactional
	public HeadlineExecution removeHeadline(Headline headline) {
		// 空值判断
		if (headline == null) {
			return new HeadlineExecution(HeadlineStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = headlineDao.deleteHeadline(headline);
			if (effectedNum <= 0) {
				throw new HeadlineOperationException("课程信息删除失败");
			} 
		} catch (Exception e) {
			throw new HeadlineOperationException("Delete Headline Information error:" + e.getMessage());
		}
		return new HeadlineExecution(HeadlineStateEnum.SUCCESS, headline);
	}

	@Override
	@Transactional
	public HeadlineExecution batchRemoveHeadline(List<Headline> headlineList) {
		// 空值判断
		if (headlineList == null) {
			return new HeadlineExecution(HeadlineStateEnum.NULL);
		}
		List<Long> headlineIDList = new ArrayList<Long>();
		for (int i=0; i<headlineList.size(); i++) {
			headlineIDList.add(headlineList.get(i).getLineID());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = headlineDao.batchDeleteHeadline(headlineIDList);
			if (effectedNum <= 0) {
				throw new HeadlineOperationException("学院头条信息批量删除失败");
			} 
		} catch (Exception e) {
			throw new HeadlineOperationException("Batch delete Headline Information error:" + e.getMessage());
		}
		HeadlineExecution exe = new HeadlineExecution(HeadlineStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		exe.setCount(effectedNum);
		return exe;
	}

}
